<?php
/* SVN FILE: $Id: cp_skin_categories.php 215 2009-09-10 19:07:19Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* ibMarket ACP category html
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 215 $
* @lastrevision $Date: 2009-09-10 19:07:19 +0000 (Thu, 10 Sep 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-09-10 19:07:19 +0000 (Thu, 10 Sep 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/skin_cp/cp_skin_categories.php $
*/

class cp_skin_categories extends output
{

/**
 * Prevent our main destructor being called by this class
 *
 * @access	public
 * @return	void
 */
public function __destruct()
{
}

/**
 * Listing all the categories
 *
 * @access	public
 * @return	string	HTML
 */
public function listCategories( array $categories ) {

$reorder = array();
for($i = 1; $i <= count($categories); $i++)
{
	$reorder[] = array($i,$i);
}
$IPBHTML .= <<<EOF
<div class='section_title'>
	<h2>{$this->lang->words['manage_categories']}</h2>
</div>

<div id='catgories'>
	<form method='post' action='{$this->settings['base_url']}module=categories&amp;section=reorder'>
		<input type='hidden' name='parent' value='{$this->request['parent']}' />
		<input type='hidden' name='_admin_auth_key' value='{$this->registry->adminFunctions->getSecurityKey()}' />
		<div class='acp-box'>
			<h3>{$this->lang->words['categories']}</h3>
			<table width='100%' cellpadding='0' cellspacing='0' border='0' class='alternate_rows'>
				<tr>
					<th style='width:1px;min-width:1%;white-space:nowrap'>{$this->lang->words['icon']}</th>
					<th style='white-space:nowrap'>{$this->lang->words['name_description']}</th>
					<th style='width:1px;min-width:1%;white-space:nowrap' title='{$this->lang->words['bpm']}'>{$this->lang->words['bpm_abbr']}</th>
					<th style='width:1px;min-width:1%;white-space:nowrap'>{$this->lang->words['cat_tax']}</th>
					<th style='width:1px;min-width:1%;white-space:nowrap'>{$this->lang->words['options']}</th>
					<th style='width:1px;min-width:1%;white-space:nowrap'>{$this->lang->words['order']}</th>
				</tr>
EOF;
foreach($categories as $category)
{
	$order = ipsRegistry::getClass('output')->formDropdown('category_order['.$category['cat_id'].']', $reorder, $category['cat_order']);
	$category['price_multiplier'] = marketFormatter::numberFormat($category['price_multiplier']);
	$category['cat_tax'] = marketFormatter::numberFormat($category['cat_tax']);
	$category['cat_description'] = IPSText::getTextClass('bbcode')->preDisplayParse($category['cat_description']);
	$IPBHTML .= <<<EOF
				<tr>
					<td><img src='{$this->settings['market_images_url']}/icons/{$category['cat_icon']}' alt='{$this->lang->words['id']}{$category['cat_id']}' /></td>
					<td>
						<strong>{$category['cat_name']}</strong>
						<br />
						{$category['cat_description']}
EOF;
	if($has_sub = !is_null(marketRegistry::getCategoryTree($category['cat_id'])))
	{
		$subcategories = array();
		foreach(marketRegistry::getCategoryTree($category['cat_id']) as $_category)
		{
			$subcategories[] = "<a href='{$this->settings['base_url']}module=categories&amp;section=manage&amp;parent=".$category['cat_id']."' style='white-space:nowrap'>".$_category['cat_name']."</a>";
		}
		$_subcategories = implode(", ",$subcategories);
		$IPBHTML .= <<<EOF
						<fieldset>
							<legend>{$this->lang->words['subcategories']}</legend>
							<div style='float:right'>
								<input class="realbutton" type="button" value="{$this->lang->words['view_items_with_subcat']}" onclick="window.location.href='{$this->settings['base_url']}module=items&section=view&category={$category['cat_id']}&showAll=1'" />
							</div>
							{$_subcategories}
						</fieldset>
EOF;
	}
	$IPBHTML .= <<<EOF
					</td>
					<td>{$category['price_multiplier']}</td>
					<td>{$category['cat_tax']}%</td>
					<td>
						<img class='ipbmenu' id="menum-{$category['cat_id']}" src='{$this->settings['skin_acp_url']}/_newimages/menu_open.png' alt='{$this->lang->words['frm_options']}' /> &nbsp;
						<ul class='acp-menu' id='menum-{$category['cat_id']}_menucontent'>
EOF;
	if($has_sub)
	{
		$IPBHTML .= <<<EOF
							<li class='icon parent'><a href='{$this->settings['base_url']}module=categories&amp;section=manage&amp;parent={$category['cat_id']}'>{$this->lang->words['view_subcategories']}</a></li>
EOF;
	}
	if(!is_null(marketRegistry::getItemsByCategory($category['cat_id'])))
	{
		$count = count(marketRegistry::getItemsByCategory($category['cat_id']));
		$IPBHTML .= <<<EOF
							<li class='icon items'><a href='{$this->settings['base_url']}module=items&amp;section=view&amp;category={$category['cat_id']}'>{$this->lang->words['view_items']} ({$count})</a></li>
EOF;
	}
	$IPBHTML .= <<<EOF
							<li class='icon edit'><a href='{$this->settings['base_url']}module=categories&amp;section=edit&amp;category={$category['cat_id']}'>{$this->lang->words['edit_category']}</a></li>
EOF;
	if(count($this->caches['market_category']) > 1)
	{
		$IPBHTML .= <<<EOF
							<li class='icon delete'><a href='{$this->settings['base_url']}module=categories&amp;section=delete&amp;category={$category['cat_id']}'>{$this->lang->words['delete_category']}</a></li>
EOF;
	}
$IPBHTML .= <<<EOF
						</ul>
					</td>
					<td>{$order}</td>
				</tr>
EOF;
}

$IPBHTML .= <<<EOF
			</table>
			<div class='acp-actionbar' style='text-align:right'>
				<div style='display: inline-block'>
EOF;
if($this->request['parent'] == -1)
{
	$IPBHTML .= <<<EOF
					<input class="realbutton" type="button" value="{$this->lang->words['view_all_items']}" onclick="window.location.href='{$this->settings['base_url']}module=items&section=view'" />
EOF;
}
else
{
	$IPBHTML .= <<<EOF
					<input class="realbutton" type="button" value="{$this->lang->words['view_items_in_parent_cat']}" onclick="window.location.href='{$this->settings['base_url']}module=items&section=view&category={$this->request['parent']}'" />
EOF;
}
$IPBHTML .= <<<EOF
				</div>
				<input class="button primary" type="submit" value="{$this->lang->words['reorder_categories']}" />
			</div>
		</div>
	</form>
</div>

EOF;

return $IPBHTML;
}

/**
 * Adding/editing category form
 *
 * @access	public
 * @return	string	HTML
 */
public function categoryForm( array $category, $nextSection, $title, $submitValue ) {

$skipCategories = array($category['cat_id']);
if(!is_null($category['cat_id']))
{
	for($i = 0; $i < count($skipCategories); $i++)
	{
		if(!is_null(marketRegistry::getCategoryTree($skipCategories[$i])))
		{
			foreach(marketRegistry::getCategoryTree($skipCategories[$i]) as $_category)
			{
				$skipCategories[] = $_category['cat_id'];
			}
		}
	}
}
$categoryList = array();
$categoryList[] = array(-1, $this->lang->words['make_root_category']);
foreach(marketRegistry::getCategoryOrder() as $categoryID)
{
	if(in_array($categoryID,$skipCategories))
	{
		continue;
	}
	$categoryList[] = array($categoryID,str_repeat("--",marketRegistry::getCategoryDepth($categoryID)).$this->caches['market_category'][$categoryID]['cat_name']);
}
$form['parent'] = ipsRegistry::getClass('output')->formDropdown('cat_parent_id', $categoryList, $category['cat_parent_id']);

$icons = array();
$defaultIcon = $category['cat_icon'];
if(!$category['cat_icon'])
{
	$iconsList = marketRegistry::getIconsList();
	$defaultIcon = $iconsList[array_rand($iconsList)];
	$icons[] = array($defaultIcon,$this->lang->words['select_icon']);
}
foreach(marketRegistry::getIconsList() as $icon)
{
	$icons[] = array($icon, $icon);
}
$form['icon'] = ipsRegistry::getClass('output')->formDropdown('cat_icon', $icons, $category['cat_icon'],'selectIcon');
$form['name'] = ipsRegistry::getClass('output')->formInput('cat_name', $category['cat_name']);

if ( IPSText::getTextClass('editor')->method == 'rte' )
{
	$description = IPSText::getTextClass('bbcode')->convertForRTE( $category['cat_description'] );
}
else
{
	$description = IPSText::getTextClass('bbcode')->preEditParse( $category['cat_description'] );
}

$form['description'] = IPSText::getTextClass('editor')->showEditor( $description, 'cat_description' );
$form['tax'] = ipsRegistry::getClass('output')->formSimpleInput('cat_tax', $category['cat_tax'])."%";
$form['bpm'] = ipsRegistry::getClass('output')->formSimpleInput('price_multiplier ', $category['price_multiplier']);

$IPBHTML .= <<<EOF
<script type='text/javascript' language='javascript'>
	ipb.vars['market_images_url'] = "{$this->settings['market_images_url']}";
</script>
<script type='text/javascript' src='{$this->settings['js_app_url']}icons.js'></script>
<div class='section_title'>
	<h2>{$title}</h2>
</div>

<div id='catgoryForm'>
	<form method='post' action='{$this->settings['base_url']}module=categories&amp;section={$nextSection}'>
		<input type='hidden' name='_admin_auth_key' value='{$this->registry->adminFunctions->getSecurityKey()}' />
		<input type='hidden' name='category' value='{$category['cat_id']}' />
		<div class='acp-box'>
			<h3>{$title}</h3>
			<ul class='acp-form alternate_rows'>
				<li>
					<label>{$this->lang->words['parent_category']}</label>
					{$form['parent']}
				</li>
				<li>
					<label>{$this->lang->words['category_icon']}</label>
					{$form['icon']} <img src='{$this->settings['market_images_url']}/icons/{$defaultIcon}' alt='{$this->lang->words['icon_alt']}' id='selectIcon_icon' />
				</li>
				<li>
					<label>{$this->lang->words['category_name']}</label>
					{$form['name']}
				</li>
				<li>
					<label>{$this->lang->words['category_description']}</label>
					<br />
					{$form['description']}
				</li>
				<li>
					<label>
						{$this->lang->words['category_tax']}
						<span class="desctext">{$this->lang->words['category_tax_description']}</span>
					</label>
					{$form['tax']}
				</li>
				<li>
					<label>
						{$this->lang->words['category_bpm']}
						<span class="desctext">{$this->lang->words['category_bpm_description']}</span>
					</label>
					{$form['bpm']}
				</li>
			</ul>
			<div class='acp-actionbar'>
				<div class='centeraction'>
					<input type='submit' value='{$submitValue}' class='button primary'>
				</div>
			</div>
		</div>
	</form>
</div>
<script type='text/javascript' language='javascript'>
	ibMarketIcons.register('selectIcon');
</script>
EOF;

return $IPBHTML;
}

/**
 * Delete category form
 *
 * @access	public
 * @return	string	HTML
 */
public function deleteCategory( array $category ) {

$skipCategories = array($category['cat_id']);
if(!is_null($category['cat_id']))
{
	for($i = 0; $i < count($skipCategories); $i++)
	{
		if(!is_null(marketRegistry::getCategoryTree($skipCategories[$i])))
		{
			foreach(marketRegistry::getCategoryTree($skipCategories[$i]) as $_category)
			{
				$skipCategories[] = $_category['cat_id'];
			}
		}
	}
}
$categoryList = array();
$categorySkipList = array();
$categorySkipList[] = array(-1, $this->lang->words['make_root_category']);
foreach(marketRegistry::getCategoryOrder() as $categoryID)
{
	$categoryList[] = array($categoryID,str_repeat("--",marketRegistry::getCategoryDepth($categoryID)).$this->caches['market_category'][$categoryID]['cat_name']);
	if(in_array($categoryID,$skipCategories))
	{
		continue;
	}
	$categorySkipList[] = array($categoryID,str_repeat("--",marketRegistry::getCategoryDepth($categoryID)).$this->caches['market_category'][$categoryID]['cat_name']);
}
$form['items'] = ipsRegistry::getClass('output')->formDropdown('new_item_cat', $categoryList, $category['cat_parent_id']);
$form['category'] = ipsRegistry::getClass('output')->formDropdown('new_parent_cat', $categorySkipList, $category['cat_parent_id']);

$IPBHTML .= <<<EOF
<div class='section_title'>
	<h2>{$this->lang->words['remove_category_title']}</h2>
</div>

<div id='deleteCategory'>
	<form method='post' action='{$this->settings['base_url']}module=categories&amp;section=delete'>
		<input type='hidden' name='_admin_auth_key' value='{$this->registry->adminFunctions->getSecurityKey()}' />
		<input type='hidden' name='category' value='{$category['cat_id']}' />
		<div class='acp-box'>
			<h3>{$this->lang->words['removing_category']}</h3>
			<ul class='acp-form alternate_rows'>
				<li>
					<label>{$this->lang->words['category_name']}</label>
					{$category['cat_name']}
				</li>
EOF;
if(!is_null(marketRegistry::getItemsByCategory($this->request['category'])))
{
	$IPBHTML .= <<<EOF
				<li>
					<label>{$this->lang->words['move_items']}</label>
					{$form['items']}
				</li>
EOF;
}
if(!is_null(marketRegistry::getCategoryTree($this->request['category'])))
{
	$IPBHTML .= <<<EOF
				<li>
					<label>{$this->lang->words['move_category']}</label>
					{$form['category']}
				</li>
EOF;
}
$IPBHTML .= <<<EOF
			</ul>
			<div class='acp-actionbar'>
				<div class='centeraction'>
					<input type='submit' value='{$this->lang->words['remove_category']}' class='button primary'>
				</div>
			</div>
		</div>
	</form>
</div>
EOF;

return $IPBHTML;
}

}